package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportLoginWxRpcService;
import com.hunttown.mes.rpc.domain.TransportLoginWxDTO;
import com.hunttown.mes.rpc.domain.query.TransportLoginWxDTOQuery;
import com.hunttown.mes.service.TransportLoginWxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportLoginWxConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:13
 */
@Service
public class TransportLoginWxRpcServiceImpl implements TransportLoginWxRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportLoginWxRpcServiceImpl.class);

    private final TransportLoginWxService transportLoginWxService;

    @Autowired
    public TransportLoginWxRpcServiceImpl(TransportLoginWxService transportLoginWxService) {
        this.transportLoginWxService = transportLoginWxService;
    }

    @Override
    public TransportLoginWxDTO insert(TransportLoginWxDTO transportLoginWxDTO) {
        try {
            return convert(transportLoginWxService.insert(convert(transportLoginWxDTO)));
        } catch (Exception e) {
            logger.error("TransportLoginWx create failed.", e);
            throw new RuntimeException("TransportLoginWx create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportLoginWxDTO transportLoginWxDTO) {
        try {
            return transportLoginWxService.updateInfoById(convert(transportLoginWxDTO));
        } catch (Exception e) {
            logger.error("TransportLoginWx update by id failed.", e);
            throw new RuntimeException("TransportLoginWx update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportLoginWxDTOQuery transportLoginWxDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportLoginWxDTOQuery.getW_id() != null && transportLoginWxDTOQuery.getW_id() > 0) {
                query.put("w_id", transportLoginWxDTOQuery.getW_id());
            }

            if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getW_openId())) {
                query.put("w_openId", transportLoginWxDTOQuery.getW_openId());
            }

            if (transportLoginWxDTOQuery.getIds() != null && !transportLoginWxDTOQuery.getIds().equals("")) {
                query.put("ids", transportLoginWxDTOQuery.getIds());
            }

            if (transportLoginWxDTOQuery.getMobile() != null) {
                query.put("mobile", transportLoginWxDTOQuery.getMobile());
            }

            if (transportLoginWxDTOQuery.getRelId() != null) {
                query.put("relId", transportLoginWxDTOQuery.getRelId());
            }

            if (transportLoginWxDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportLoginWxDTOQuery.getStopFlag());
            }

            if (transportLoginWxDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportLoginWxDTOQuery.getDeleteFlag());
            }

            return transportLoginWxService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportLoginWx update by query failed.", e);
            throw new RuntimeException("TransportLoginWx update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportLoginWxDTO selectById(Integer id) {
        try {
            return convert(transportLoginWxService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportLoginWx select by id failed.", e);
            throw new RuntimeException("TransportLoginWx select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportLoginWxDTO selectObjByQuery(TransportLoginWxDTOQuery transportLoginWxDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportLoginWxDTOQuery.getEntId() != null) {
                query.put("entId", transportLoginWxDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getOpenId())) {
                query.put("openId", transportLoginWxDTOQuery.getOpenId());
            }

            if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getUnionId())) {
                query.put("unionId", transportLoginWxDTOQuery.getUnionId());
            }

            if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getMobile())) {
                query.put("mobile", transportLoginWxDTOQuery.getMobile());
            }

            if (transportLoginWxDTOQuery.getOriginType() != null) {
                query.put("originType", transportLoginWxDTOQuery.getOriginType());
            }

            if (transportLoginWxDTOQuery.getUserType() != null) {
                query.put("userType", transportLoginWxDTOQuery.getUserType());
            }

            if (transportLoginWxDTOQuery.getRelId() != null) {
                query.put("relId", transportLoginWxDTOQuery.getRelId());
            }

            if (transportLoginWxDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportLoginWxDTOQuery.getStopFlag());
            }

            if (transportLoginWxDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportLoginWxDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getOrderBy())) {
                query.put("orderBy", transportLoginWxDTOQuery.getOrderBy());
            }

            return convert(transportLoginWxService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportLoginWx select obj by query failed.", e);
            throw new RuntimeException("TransportLoginWx select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportLoginWxDTO> getListForPage(TransportLoginWxDTOQuery transportLoginWxDTOQuery) {
        Query query = Query.page(transportLoginWxDTOQuery.getPn(), transportLoginWxDTOQuery.getPageSize());

        if (transportLoginWxDTOQuery.getId() != null && transportLoginWxDTOQuery.getId() > 0) {
            query.put("id", transportLoginWxDTOQuery.getId());
        }

        if (transportLoginWxDTOQuery.getIds() != null && !transportLoginWxDTOQuery.getIds().equals("")) {
            query.put("ids", transportLoginWxDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getLikeName())) {
            query.put("likeName", transportLoginWxDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getLikeNickName())) {
            query.put("likeNickName", transportLoginWxDTOQuery.getLikeNickName());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getOpenId())) {
            query.put("openId", transportLoginWxDTOQuery.getOpenId());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getUnionId())) {
            query.put("unionId", transportLoginWxDTOQuery.getUnionId());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getMobile())) {
            query.put("mobile", transportLoginWxDTOQuery.getMobile());
        }

        if (transportLoginWxDTOQuery.getHasMobile() != null && transportLoginWxDTOQuery.getHasMobile() == 1) {
            query.put("hasMobile", transportLoginWxDTOQuery.getHasMobile());
        }

        if (transportLoginWxDTOQuery.getOriginType() != null && transportLoginWxDTOQuery.getOriginType() != -9999) {
            query.put("originType", transportLoginWxDTOQuery.getOriginType());
        }

        if (transportLoginWxDTOQuery.getUserType() != null && transportLoginWxDTOQuery.getUserType() != -9999) {
            query.put("userType", transportLoginWxDTOQuery.getUserType());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getShowDate())) {
            query.put("showDate", transportLoginWxDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getStartDate())) {
            query.put("startDate", transportLoginWxDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getEndDate())) {
            query.put("endDate", transportLoginWxDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportLoginWxDTOQuery.getOrderBy())) {
            query.put("orderBy", transportLoginWxDTOQuery.getOrderBy());
        }

        if (transportLoginWxDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportLoginWxDTOQuery.getStopFlag());
        }

        if (transportLoginWxDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportLoginWxDTOQuery.getDeleteFlag());
        }

        List<TransportLoginWxDTO> list = convert(transportLoginWxService.selectByQuery(query));
        Page<TransportLoginWxDTO> page = Page.from(transportLoginWxDTOQuery.getPn(), transportLoginWxDTOQuery.getPageSize(), list);
        page.setTotalItems(transportLoginWxService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportLoginWxService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportLoginWx delete failed.", e);
            throw new RuntimeException("TransportLoginWx delete failed, Please order your code.", e);
        }
    }

    @Override
    public String getWxOpenIdByUserId(Integer userId) {
        try {
            return transportLoginWxService.getWxOpenIdByUserId(userId, -1);
        } catch (Exception e) {
            logger.error("TransportLoginWx delete failed.", e);
            throw new RuntimeException("TransportLoginWx get openid failed, Please order your code.", e);
        }
    }

    @Override
    public String getWxOpenIdByUserId(Integer userId, Integer entId) {
        try {
            return transportLoginWxService.getWxOpenIdByUserId(userId, entId);
        } catch (Exception e) {
            logger.error("TransportLoginWx delete failed.", e);
            throw new RuntimeException("TransportLoginWx get openid failed, Please order your code.", e);
        }
    }
}